package com.etalk.crm.controller.lesson;

import com.alibaba.fastjson.JSON;
import com.etalk.crm.pojo.ClassBookingAutoRecord;
import com.etalk.crm.service.ClassBookingAutoRecordService;
import com.etalk.crm.service.StudentInfoService;
import com.etalk.crm.utils.PojoMapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jordan
 */
@Controller
@RequestMapping("/class")
public class ClassBookingAutoController {

    @Resource
    private ClassBookingAutoRecordService classBookingAutoRecordService;
    @Resource
    private StudentInfoService studentInfoService;

    @Value("${interface.booked.courese.url}")
    String bookedUrl;

    /**
     * 约课计划列表
     * @param model 页面对象
     * @param record 查询条件
     * @param pageNum 页码
     * @param pageSize 页记录
     * @return
     */
    @RequestMapping(value = "/booking/auto/page",method = {RequestMethod.GET,RequestMethod.POST})
    public String getListByPage(Model model, ClassBookingAutoRecord record, Integer pageNum, Integer pageSize){

        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        model.addAttribute("pageInfo",classBookingAutoRecordService.searchClassBookingAutoRecordListByPage(record, pageNum, pageSize));

        return "lesson/class_booking_auto_list";
    }

    /**
     * 打开添加约课计划页面
     * @param model 页面数据
     * @return
     */
    @GetMapping(value = "/booking/auto/edit")
    public String getClassBooking(Model model,ClassBookingAutoRecord record){
        model.addAttribute("bookedUrl",bookedUrl);
        model.addAllAttributes(PojoMapUtils.convert2Map(record));
        return "lesson/class_booking_auto_add";
    }

    /**
     * 获取学员tokenString
     * @param personId 学员id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/token/{personId}",produces = "application/json; charset=utf-8")
    public String getTokenStringByStudentId(@PathVariable("personId") Integer personId){
        Map<String,Object> resultMap =new HashMap<>(2);
        String token=studentInfoService.getStudentTokenString(personId);
        if (StringUtils.isEmpty(token)){
            resultMap.put("status",0);
        }else {
            resultMap.put("status",1);
            resultMap.put("tokenString",token);
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 添加约课计划计划
     * @param record 添加信息
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/booking/auto/add",produces = "application/json; charset=utf-8")
    public String setClassChangeInfo(ClassBookingAutoRecord record){

        Map<String,Object> resultMap = classBookingAutoRecordService.addClassBookingAutoRecord(record);

        return JSON.toJSONString(resultMap);
    }

    /**
     * 取消约课计划记录
     * @param id 调课记录id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/booking/auto/cancel/{id}",produces = "application/json; charset=utf-8")
    public String cancelClassChangeInfo(@PathVariable("id") int id){

        Map<String,Object> resultMap = classBookingAutoRecordService.cancelClassBookingAutoRecordById(id);

        return JSON.toJSONString(resultMap);
    }

    /**
     * 批量取消约课计划记录
     * @param listId 调课记录id
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/booking/auto/batch/cancel",produces = "application/json; charset=utf-8")
    public String cancelBatchClassChangeInfo(@RequestParam("listId") List<Integer> listId){

        Map<String,Object> resultMap = classBookingAutoRecordService.cancelClassBookingAutoRecordById(listId);

        return JSON.toJSONString(resultMap);
    }
}
